package com.sgcc.fj.pmos.e_fj_order.activity;


import android.content.Intent;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.flyco.tablayout.SlidingTabLayout;
import com.sgcc.fj.pmos.arouter.order.OrderArouterPath;
import com.sgcc.fj.pmos.common.adapter.HomeTabPagerAdapter;
import com.sgcc.fj.pmos.common.base.BaseActivity;
import com.sgcc.fj.pmos.common.utils.ImmersionBarUtils;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.fj.pmos.e_fj_home.activity.MembersMarketSearchActivity;
import com.sgcc.fj.pmos.e_fj_order.fragment.ContractSearchFragment;
import com.sgcc.fj.pmos.e_fj_order.fragment.IndentSearchFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * @author rjc
 * @describe 商品、店铺搜索
 */

@Route(path = OrderArouterPath.ORDER_SEARCHORER)
public class OrderSearchActivity extends BaseActivity {
    private SlidingTabLayout mStlContent;
    private ViewPager mViewpager;
    private final List<Fragment> fragments = new ArrayList<>();
    private HomeTabPagerAdapter homeTabPagerAdapter;
    private TextView search_txt;
    private EditText search_edit;
    private String inputContent;
    Intent intent;
    int searchType;
    private LinearLayout content_linner;
    private ImageView back_img;
    IndentSearchFragment indentFragment = new IndentSearchFragment();
    ContractSearchFragment contractFragment = new ContractSearchFragment();

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_order_search;
    }

    @Override
    protected void initViews() {
        super.initViews();
        intent = getIntent();
        back_img = findViewById(R.id.back_img);
        back_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                OrderSearchActivity.this.finish();
            }
        });
        content_linner = findViewById(R.id.content_linner);
        searchType = intent.getIntExtra("searchType", 0);
        ImmersionBarUtils.initTransparentNavigation(this);
        search_txt = findViewById(R.id.search_txt);
        search_edit = findViewById(R.id.search_edit);
        search_txt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                inputContent = search_edit.getText().toString();
                content_linner.setVisibility(View.VISIBLE);
                indentFragment.setSearchContent(inputContent);
                contractFragment.setSearchContent(inputContent);
            }
        });

        search_edit.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                    //先隐藏键盘
                    KeyboardUtils.hideSoftInput(OrderSearchActivity.this);
                    //其次再做相应操作
                    inputContent = search_edit.getText().toString();
                    if (TextUtils.isEmpty(inputContent)) {
                        ToastUtils.showShort("请输入搜索内容");
                    } else {
                        //做相应的操作
                        content_linner.setVisibility(View.VISIBLE);
                        content_linner.setVisibility(View.VISIBLE);
                        indentFragment.setSearchContent(inputContent);
                        contractFragment.setSearchContent(inputContent);
                    }
                }
                return false;
            }
        });

        mStlContent = findViewById(R.id.stl_content);
        mViewpager = findViewById(R.id.viewpager);
        String[] titleList = getResources().getStringArray(R.array.order_title);
        fragments.add(indentFragment);
        fragments.add(contractFragment);
        homeTabPagerAdapter = new HomeTabPagerAdapter(getSupportFragmentManager(), fragments);
        homeTabPagerAdapter.setmBottomTabNames(titleList);
        mViewpager.setOffscreenPageLimit(2);
        mViewpager.setAdapter(homeTabPagerAdapter);
        mStlContent.setViewPager(mViewpager);
        mViewpager.setCurrentItem(searchType);
        mViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }


    @Override
    protected void initData() {
        super.initData();

    }


}